package com.kim.rssreader.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.kim.rssreader.utils.proxy.NetProxyInfo;
import com.kim.rssreader.utils.proxy.NetProxyInfoProxy;

public class NetUtil {

	public interface NetProcessComplete {
		void onProcess(InputStream inputStream);
	}

	/**
	 * 检测当前网络是否可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetAvailable(Context context) {

		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivityManager == null) {
			return false;
		}

		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null) {
			return false;
		}

		if (NetworkInfo.State.CONNECTED == networkInfo.getState()) {
			return true;
		}
		return false;
	}

	/**
	 * 获取URL连接
	 * 
	 * @param urlStr
	 *            url地址
	 * @return HttpURLConnection
	 * @throws IOException
	 */
	public static final HttpURLConnection getHttpURLConnection(String urlStr)
			throws IOException {
		URL url = new URL(urlStr);
		NetProxyInfo proxyInfo = NetProxyInfoProxy.getInstance()
				.getNetProxyInfo();
		HttpURLConnection httpconn;
		if (proxyInfo == null || null != proxyInfo.host) {
			httpconn = (HttpURLConnection) url.openConnection();
			System.out
					.println("httpconn = (HttpURLConnection) url.openConnection();");
		} else {
			Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
					proxyInfo.host, proxyInfo.port));
			httpconn = (HttpURLConnection) url.openConnection(proxy);
			System.out
					.println("Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(");
		}
		return httpconn;
	}

	/**
	 * 开始抓取数据
	 * 
	 * @param httpURLConnection
	 * @return
	 * @throws IOException
	 */
	public static final String startFetchData(
			HttpURLConnection httpURLConnection) {

		if (httpURLConnection == null) {
			return null;
		}

		String content = null;
		try {
			httpURLConnection.connect();
			if (200 == httpURLConnection.getResponseCode()) {
				content = RssSourceReader.inputStream2String(httpURLConnection
						.getInputStream());
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			httpURLConnection.disconnect();
		}
		return content;
	}
	

	/**
	 * 获取并处理网络输入流
	 * 
	 * @param url
	 * @throws IOException
	 */
	public static final void getInputStream(String url,
			NetProcessComplete processComplete) {
		HttpURLConnection connection = null;
		InputStream inputStream = null;
		try {
			connection = getHttpURLConnection(url);
			connection.connect();
			if (200 == connection.getResponseCode()) {
				inputStream = connection.getInputStream();
				processComplete.onProcess(inputStream);
			}else{
				processComplete.onProcess(null);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
					inputStream = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (connection != null) {
				connection.disconnect();
			}
		}
	}

	/**
	 * 将输入流转换为字符串
	 * 
	 * @param inputStream
	 * @return {@link String}
	 * @throws IOException
	 */
	public static final String convertInputStream2String(InputStream inputStream)
			throws IOException {
		if (inputStream == null || inputStream.available() == 0) {
			return null;
		}
		int len = -1;
		byte[] buf = new byte[1024];
		StringBuilder builder = new StringBuilder(inputStream.available());
		while ((len = inputStream.read(buf)) != -1) {
			builder.append(new String(buf, 0, len));
		}
		return builder.toString();
	}

	/**
	 * 将输入流转换为字符串
	 * 
	 * @param inputStream
	 * @return {@link String}
	 * @throws IOException
	 */
	public static final String convertInputStream2String(
			InputStream inputStream, String encoding) throws IOException {
		if (inputStream == null || inputStream.available() == 0) {
			return null;
		}
		int len = -1;
		byte[] buf = new byte[1024];
		StringBuilder builder = new StringBuilder(inputStream.available());
		while ((len = inputStream.read(buf)) != -1) {
			builder.append(new String(buf, 0, len, encoding));
		}
		return builder.toString();
	}
}
